﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using DepInnerTask.Common.Interface;
using DepInnerTask.Common.Entity;
using Common.Service;

namespace DepInnerTask
{
    
        public partial class MyProcess : System.Web.UI.Page
    {
        ITasks task = ServiceContainer.GetService<ITasks>();
        //ITaskStatus taskStatus = ServiceContainer.GetService<ITaskStatus>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDropList();
                BindDropListFish();
                Index = "0";
                LoadpageDate(0);
                LoadFinPageData(0);   
            }
        }
        public string Index { get; set; }

        #region 绑定所有流程
        private void BindDropList()
        {
            this.ddlProcessCatalog.DataSource = task.GetList();
            this.ddlProcessCatalog.DataTextField = "PROCESSNAME";
            this.ddlProcessCatalog.DataValueField = "TASKID";
            this.ddlProcessCatalog.DataBind();
            this.ddlProcessCatalog.Items.Insert(0, new ListItem("所有...", "0"));
        }
        #endregion
        #region 绑定所有流程
        private void BindDropListFish()
        {
            this.ddlFinProcess.DataSource = task.GetList();
            this.ddlFinProcess.DataTextField = "PROCESSNAME";
            this.ddlFinProcess.DataValueField = "TASKID";
            this.ddlFinProcess.DataBind();
            this.ddlFinProcess.Items.Insert(0, new ListItem("所有...", "0"));
        }
        #endregion
        private void LoadpageDate(int index)
        {
            int pageSize = 10;
            int count = 0;
            string whereStr = "";
            if (!string.IsNullOrEmpty(processName.Value))
            {
                whereStr += "PROCESSNAME▽" + processName.Value + "△";
            }
            if (!string.IsNullOrEmpty(bussinessID.Value))
            {
                whereStr += "SUMMARY▽" + bussinessID.Value + "△";
            }
            if (!string.IsNullOrEmpty(applicantUser.Value))
            {
                whereStr += "ASSIGNEDTOUSER▽" + applicantUser.Value + "△";//当前处理人
            }
            if (!String.IsNullOrEmpty(startDate.Value))
            {
                whereStr += "starttime▽" + startDate.Value + "△";
            }
            if (!String.IsNullOrEmpty(endDate.Value))
            {
                whereStr += "endtime▽" + endDate.Value + "△";
            }
            if (ddlProcessCatalog.SelectedValue != "0")
            {
                whereStr += "ProcessNAME▽" + ddlProcessCatalog.SelectedItem.Text + "△";
            }
            if (ddlTaskStatus.SelectedValue != "0")
            {
                whereStr += "status▽" + ddlTaskStatus.SelectedItem.Text  + "△";
            }
            IList list = task.GetList(whereStr, 0, 10, out count);
            PagedDataSource page = new PagedDataSource();

            int totalCount = list.Count;
            //DataView dv = dt.DefaultView;
            page.DataSource = list;
            page.AllowPaging = true;
            page.PageSize = pageSize;
            page.CurrentPageIndex = index;

            rpMyTask.DataSource = page;
            rpMyTask.DataBind();

            pagination.RecordCount = totalCount;
            pagination.PageSize = page.PageSize;
            pagination.CurrentPageIndex = page.CurrentPageIndex + 1;
            pagination.Visible = true;
            pagination.AlwaysShow = true;
            pagination.CurrentPageButtonClass = "btnPageNavCurrentInput";
            pagination.DataBind();
        }


        #region 已办
        private void LoadFinPageData(int index)
        {
            int pageSize = 10;
            int count = 0;
            string whereStr = "";
            if (!string.IsNullOrEmpty(processNameCom.Value))
            {
                whereStr += "PROCESSNAME▽" + processNameCom.Value + "△";
            }
            if (!string.IsNullOrEmpty(bussinessIDCom.Value))
            {
                whereStr += "SUMMARY▽" + bussinessIDCom.Value + "△";
            }
            if (!string.IsNullOrEmpty(applicantUserCom.Value))
            {
                whereStr += "initiator▽" + applicantUserCom.Value + "△";
            }
            if (!String.IsNullOrEmpty(startDateCom.Value))
            {
                whereStr += "starttime▽" + startDateCom.Value + "△";
            }
            if (!String.IsNullOrEmpty(endDateCom.Value))
            {
                whereStr += "endtime▽" + endDateCom.Value + "△";
            }
            if (ddlFinProcess.SelectedValue != "0")
            {
                whereStr += "ProcessNAME▽" + ddlFinProcess.SelectedItem.Text + "△";
            }
            if (ddlStatusCom.SelectedValue != "0")
            {
                whereStr += "status▽" + ddlStatusCom.SelectedItem.Text + "△";
            }
            IList list = task.GetList(whereStr, 0, 10, out count);
            PagedDataSource page1 = new PagedDataSource();

            int totalCount = list.Count;
            //DataView dv = dt.DefaultView;
            page1.DataSource = list;
            page1.AllowPaging = true;
            page1.PageSize = pageSize;
            page1.CurrentPageIndex = index;

            rpCompleteTask.DataSource = page1;
            rpCompleteTask.DataBind();

            AspNetPager1.RecordCount = totalCount;
            AspNetPager1.PageSize = page1.PageSize;
            AspNetPager1.CurrentPageIndex = page1.CurrentPageIndex + 1;
            AspNetPager1.Visible = true;
            AspNetPager1.AlwaysShow = true;
            AspNetPager1.CurrentPageButtonClass = "btnPageNavCurrentInput";
            AspNetPager1.DataBind();
        }
        private string GetFinWhereCondition()
        {
            string returnString = String.Empty;

            return returnString;
        }
        #endregion

        /// <summary>
        /// 获取流程状态
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public string GetStatusString(string endtime)
        {
            string returnString = "";
            DateTime dueTime = DateTime.Parse(endtime);//截止时间
            DateTime nowTime = DateTime.Now;//当前时间
            TimeSpan ts = dueTime - nowTime;
            if (ts.Seconds < 0)//超期
            {
                returnString += "超期";
                if (Math.Abs(ts.Days) >= 1)
                {
                    returnString += Math.Abs(ts.Days) + "天" + Math.Abs(ts.Hours) + "小时";
                }
                else
                {
                    returnString += Math.Abs(ts.Hours) + "小时";
                }
            }
            else
            {
                if (ts.Days >= 1)
                {
                    returnString += ts.Days + "天" + ts.Hours + "小时";
                }
                else
                {
                    returnString += ts.Hours + "小时";
                }
                returnString += "到期";
            }
            return returnString;
        }

        /// <summary>
        /// 分页选择
        /// </summary>
        /// <param name="src"></param>
        /// <param name="e"></param>
        protected void pagination_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            LoadpageDate(e.NewPageIndex - 1);
            Index = "0";
        }

        /// <summary>
        /// 分页选择
        /// </summary>
        /// <param name="src"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            //LoadPageData(e.NewPageIndex - 1);
            LoadFinPageData(e.NewPageIndex - 1);
            Index = "1";
        }



        protected void btnSearchCom_Click(object sender, EventArgs e)
        {
            LoadFinPageData(0);
            Index = "1";
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadpageDate(0);
            Index = "0";
        }
    }
}